package taliav3;
import java.util.*;
import javax.swing.*;

/*
 *  legenda: 
 *  (H)earts      (kier/serce)
 *  (D)iamonds    (karo/dzwonek)
 *  (S)pades      (pik/wino)
 *  (C)lubs       (trefl/żołądź)
 *
 * Klasa definiujaca talie.
 * Podstawowe wlasciwosci talii
 * tworzenie talii (w oparciu o ustawienia)
 * rozdawanie kart
 * wydawanie kart
 * tasowanie kart
 *
 */

public class Dealer
{
    /** Variable which stores Cards already used during play.*/
    protected ArrayList <Card> Used;
    /** Used to store Card ready to get when variable get>0 or when player wants to.*/
    protected ArrayList <Card> Decks;
    /** Card type variable storing Card laying on top.*/
    protected Card ActiveCard;
    protected char Colors[] /** Char type table to store colors of Card.*/, demand /** Char variable stores wheter is a demand (rank or figure).*/;
    protected int dim2, dim3, jokers_amount, available, players, demandQueue, stopQueue, get;
    /** Storing path to imagefile of the specified reverse.*/
    protected String rewers;
    public Dealer (int amount, int jokers, int taliaI)
    {
        get=0;
        Decks = new ArrayList<Card>();
        Used = new ArrayList<Card>();
        demand = '0';
        demandQueue = 0;
        stopQueue = 0;
        Colors = new char[]{'H','D','S','C'};
        dim2=52;
        available=52;
        dim3=13;
        jokers_amount=jokers;
        if(jokers>0)
        {
            dim2+=jokers;
            available+=jokers;
            dim3++;
        }
        ShuffleDeck(taliaI);
    }
    private void ShuffleDeck(int taliaI)
    {
        int minus=0;
        if(jokers_amount!=0)
            minus++;
        for(int i=0; i<dim3-minus; i++)
        {
            for(int j=0; j<4; j++)
            {
                int tPunish=0;
                char tRank='0';
                tRank = Card.SetRank(i);
                tPunish = Card.SetPunish(tRank, Colors[j]);
                String ico = Card.ImagePath(Colors[j], tRank, taliaI);
                Decks.add(new Card(tRank,Colors[j],tPunish,ico,false));
                Decks.get(i).setIcon(new ImageIcon(getClass().getResource(ico)));
            }
        }
        if(jokers_amount!=0)
        {
            for(int i=0; i<jokers_amount; i++)
                Decks.add(new Card('B','\0',0,Card.ImagePath('\0','B',taliaI),true));
        }
        Collections.shuffle(Decks);
        Collections.shuffle(Decks);        
    }
    public void GiveOutDeck(Player tPlayer, int player_amount, int player, int perPlayer)
    {
        players=player_amount;
        int index=1;
        for(int i=Decks.size()-player; index<=perPlayer; i-=player_amount,index++)
        {
            tPlayer.add(Decks.get(i));
            tPlayer.getCard(tPlayer.getPlayerCardsAmount()-1).ind=tPlayer.getPlayerCardsAmount()-1;
            available--;
        }
    }
    public void Begin()
    {
        Decks.subList(available, Decks.size()).clear();
        ActiveCard = new Card();
        available--;
        while(Decks.get(available).punish!=0 || Decks.get(available).rank=='B')
            available--;
        ActiveCard = Decks.get(available);
        Decks.remove(available);
    }
    public int deckSize()
    {   return Decks.size();    }
    public Card getActiveCard()
    {   return ActiveCard;  }
    public void setActiveCard(Card Active)
    {
        ActiveCard.color = Active.color;
        ActiveCard.rank = Active.rank;
        ActiveCard.punish = Active.punish;
        ActiveCard.pathAwers = Active.pathAwers;
        ActiveCard.joker = Active.joker;
    }
    public void remove(int index)
    {   Decks.remove(index);    }
    protected Card getCard(int index)
    {   return Decks.get(index);    }
    public void add(Card Card1)
    {   Used.add(Card1); }
    public void RemoveSubList(int min, int max)
    {
        for(int i=max; i>=min; i--)
            Decks.remove(i);
    }
}